package com.lxw.manage_cms.controller;


import com.lxw.api.cms.CmsPageControllerApi;
import com.lxw.framework.domain.cms.CmsPage;
import com.lxw.framework.domain.cms.request.QueryPageRequest;
import com.lxw.framework.domain.cms.response.CmsPageResult;
import com.lxw.framework.model.response.CommonCode;
import com.lxw.framework.model.response.QueryResponseResult;
import com.lxw.framework.model.response.QueryResult;
import com.lxw.manage_cms.service.ICmsPageService;
import com.lxw.manage_cms.service.ICmsSiteService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("page")
public class CmsPageController implements CmsPageControllerApi {

    @Autowired
    ICmsPageService cmsPageService;


    @Override
    @GetMapping("/list/{page}/{pageSize}")
    public QueryResponseResult findList(@PathVariable Integer page, @PathVariable Integer pageSize, QueryPageRequest queryPageRequest) {

        QueryResult<CmsPage> queryResult = new QueryResult();
        Page<CmsPage> cmsPages = cmsPageService.findList(page,pageSize,queryPageRequest);

        queryResult.setList(cmsPages.getContent()); //每页的数据
        queryResult.setTotal(cmsPages.getTotalElements());//总条数

        QueryResponseResult queryResponseResult = new QueryResponseResult(CommonCode.SUCCESS,queryResult);

        return queryResponseResult;
    }

    @Override
    @PostMapping("add")
    public CmsPageResult add(@RequestBody CmsPage cmsPage) {
        //非空校验
        if (StringUtils.isEmpty(cmsPage.getPageName())){
            throw new RuntimeException("页面名称不能为空");
        }
        //业务校验和数据插入
        cmsPageService.save(cmsPage);
        CmsPageResult cmsPageResult = new CmsPageResult(CommonCode.SUCCESS,null);
        return cmsPageResult;
    }

}
